/**
 * The appointment class stores all the information for an appointment,
 * including when the appointment is, the preferred doctor, and the reason for the visit.
 * When initially created, it is unapproved.
 * A doctor, nurse, or system administrator can change paramters and approve the appointment.
 * @author Owen Cox
 *
 */
public class Appointment {
	private String reason;
	private String doctor;
	boolean approved;
	private int minute,day,month,year;
	int length;
	User requester;
	/**
	 * Standard constructor that sets all parameters.
	 * @param length	length of the visit, in minutes
	 * @param reason	reason for the visit
	 * @param prefDoctor	preferred doctor
	 * @param minute	time of day, in minutes
	 * @param day	which day
	 * @param month	which month
	 * @param year	which year
	 */
	public Appointment(int length,  String prefDoctor, String reason, 
			int minute, int day, int month, int year, User u){
		this.minute=minute;
		this.reason = reason;
		this.day=day;
		this.month=month;
		this.year=year;
		this.length=length;
		this.doctor=prefDoctor;
		this.requester = u;
		approved=false;
	}
	/**
	 * A second constructor with the default length.
	 * @param reason
	 * @param prefDoctor
	 * @param minute
	 * @param hour
	 * @param day
	 * @param month
	 * @param year
	 */
	public Appointment(String reason, String prefDoctor, 
			int minute, int day, int month, int year, User u){
		this(60, prefDoctor, reason, minute, day, month, year, u);
	}
	/**
	 * Sets the time of the appointment.
	 * @param minute 	What time (hours should be converted to minutes)
	 * @param day	Which day
	 * @param month	What month
	 * @param year	What year
	 */
	public void setTime(int minute, int day, int month, int year){
		this.day=day;
		this.month=month;
		this.year=year;
	}
	/**
	 * Sets the length of the appointment, in minutes.
	 * @param length
	 */
	public void setLength(int length){
		this.length=length;
	}
	/**
	 * approves the appointment
	 */
	public void approve(){
		this.approved=true;
	}
	/**
	 * return the reason for the visit
	 * @return reason
	 */
	public String getReason(){
		return reason;
	}
	/**
	 * return the name of the preferred doctor
	 * @return preferred doctor
	 */
	public String getPreferredDoctor(){
		return doctor;
	}
	/**
	 * Gets the time of day in minutes for the appointment.
	 * @return minutes
	 */
	public int getMinute(){
		return minute;
	}
	/**
	 * Returns the day of the appointment
	 * @return day
	 */
	public int getDay(){
		return day;
	}
	/**
	 * returns the month of the appointment
	 * @return month
	 */
	public int getMonth(){
		return month;
	}
	/**
	 * returns the year of the appointment
	 * @return year
	 */
	public int getYear(){
		return year;
	}
	/**
	 * If the appointment has not yet been approved, the doctor is the preferred doctor.
	 * Once the appointment has been reviewed and approved, the doctor is the doctor who will
	 * be fulfilling the appointment.
	 * @return doctor.
	 */
	public String getDoctor(){
		return doctor;
	}
	/**
	 * @return the length of the appointment
	 */
	public int getLength(){
		return length;
	}
	/**
	 * Returns true if a nurse, doctor, or system admin has approved the appointment
	 * @return approved
	 */
	public boolean isApproved(){
		return approved;
	}
	/**
	 * Creates a string version of the class so it can be stored.
	 */
	public String toString(){
		return length+"\t" +
			doctor + "\t" +
			reason + "\t" + 
			minute + "\t" +
			day + "\t" +
			month + "\t" +
			year + "\t" + 
			requester.userID;
		}
	/**
	 * Creates an appointment from the toString of an appointment
	 * @param appointment
	 */
	public Appointment(String appointment){
		try{
			String[] args = appointment.split("\t");
			
			doctor=args[1];
			reason=args[2];
			try{
				this.length=Integer.parseInt(args[0]);  
				this.minute=Integer.parseInt(args[3]);
				this.day=Integer.parseInt(args[4]); 
				this.month=Integer.parseInt(args[5]); 
				this.year=Integer.parseInt(args[6]);
				this.requester = DatabaseUsers.getUser(Integer.parseInt(args[7]));
			}
			catch(Exception e){
				this.length=0;  
				this.minute=0;
				this.day=0; 
				this.month=0; 
				this.year=0;
				this.requester = DatabaseUsers.getUser(0);
			}
		}
		catch(Exception e){
			System.out.println("error parsing appointment");
			Logger.log("Error parsing appointment");
		}
	}
	/**
	 * Creates an appointment from the toString of an appointment
	 * @param appointment
	 */
	public static Appointment fromString(String appointment){
		
		try{
			String[] args = appointment.split("\t");
			Appointment a = new Appointment(
			Integer.parseInt(args[0]),  
			args[1],
			args[2], 
			Integer.parseInt(args[3]),
			Integer.parseInt(args[4]),
			Integer.parseInt(args[5]),
			Integer.parseInt(args[6]),
			DatabaseUsers.getUser(Integer.parseInt(args[7])));
			return a;
		}
		catch(Exception e){
			System.out.println("Error parsing appointment: "+appointment);
			Logger.log("Error parsing appointment: "+appointment);
			return null;
		}
	}
}
